import { getAction, putAction, postAction } from '@/api/manage'
import { api as userApi } from '../user/service.js'
import { serviceApi } from '@/api/common.js'
/**
 * 公共API接口
 */
export const api = {
  list: serviceApi.auth + '/roles',
  delete: (ids) => serviceApi.auth + `/roles/${ids}`,
  userList: userApi.userList
}

/**
 * 添加角色
 * @param params
 * @returns {AxiosPromise}
 */
export const addRole = params => postAction(serviceApi.auth + '/roles', params)

/**
 * 编辑角色
 * @param {*} params
 * @returns
 */
export const editRole = params => putAction(serviceApi.auth + '/roles', params)

/**
 * 查询所有角色
 * @param params
 * @returns {AxiosPromise}
 */
export const queryAllRole = params => getAction(serviceApi.auth + '/roles/list/all', params)

/**
* 查询指定角色菜单权限下的数据规则
* @param params
* @returns {AxiosPromise}
*/
export const loadDataRulePermissionId = (roleId, permissionId, params) => getAction(serviceApi.auth + `/roles/${roleId}/permissions/${permissionId}/data-rules`, params)

/**
 * 保存数据规则至角色菜单关联表
 * @param {*} roleId 角色ID
 * @param {*} permissionId 菜单ID
 * @param {*} params 数据
 * @returns
 */
export const saveDataRulePermissionId = (roleId, permissionId, params) => postAction(serviceApi.auth + `/roles/${roleId}/permissions/${permissionId}/data-rules`, params)

/**
 * 获取角色下应用菜单权限树
 * @param {*} appId 应用ID
 * @param {*} params 参数
 * @returns
 */
export const queryTreeListForRole = (appId, params) => getAction(serviceApi.auth + `/roles/apps/${appId}`, params)

/**
 * 获取租户下应用菜单权限树
 * @param {*} tenantId 租户ID
 * @param {*} appId 应用ID
 * @param {*} params 参数
 * @returns
 */
export const tenantAppTree = (tenantId, appId, params) => getAction(serviceApi.auth + `/roles/tenants/${tenantId}/apps/${appId}`, params)

/**
 * 通过类型获取应用列表
 * 获取角色分配的应用授权树
 * @param {*} roleId 角色ID
 * @param {*} params 参数
 * @returns
 */
export const queryAppTreeByRoleId = (roleId, tenantId, params) => getAction(serviceApi.auth + `/roles/${roleId}/tenants/${tenantId}/apps/trees`, params)

/**
 * 获取租户下角色以及分配的应用树
 * @param {*} tenantId 租户ID
 * @param {*} params 参数
 * @returns
 */
export const simpleTenantRoleTree = (tenantId, params) => getAction(serviceApi.auth + `/roles/simple-tenant-role-tree/${tenantId}`, params)

/**
 * 获取角色下已选中的App
 * @param {*} tenantId 租户ID
 * @param {*} roleId 角色ID
 * @param {*} params 参数
 * @returns
 */
export const simpleTenantRoleAppTree = (tenantId, roleId, params) => getAction(serviceApi.auth + `/roles/${roleId}/simple-tenant-role-tree/${tenantId}/apps`, params)

/**
 * 租户-角色-应用 权限
 * 获取租户下对应角色应用权限
 * @param {*} tenantId 租户ID
 * @param {*} roleId 角色ID
 * @param {*} appId 应用ID
 * @param {*} params 参数
 * @returns
 */
export const queryTenantRoleAppTree = (tenantId, roleId, appId, params) => getAction(serviceApi.auth + `/roles/${roleId}/tenants/${tenantId}/apps/${appId}`, params)
/**
 * 添加用户
 * @param {*} params
 * @returns
 */
export const addUser = params => postAction(serviceApi.auth + '/users', params)
/**
 * 编辑用户
 * @param {*} params
 * @returns
 */
export const editUser = params => putAction(serviceApi.auth + '/users', params)

/**
 * 通过userId获取用户角色ID列表
 * @param {*} userId
 * @param {*} params
 * @returns
 */
export const queryUserRole = (userId, params) => getAction(serviceApi.auth + `/users/${userId}/roles/ids`, params)

/**
 * 查询用户部门
 * @param params
 * @returns {AxiosPromise}
 */
export const getUserDepartIdsList = (userId, params) => getAction(serviceApi.auth + `/users/${userId}/departs/ids`, params)

/**
 * 应用授权信息保存
 * @param params
 * @returns {AxiosPromise}
 */
export const saveRoleAppPermission = params => postAction(serviceApi.auth + '/permissions/default-role-permission', params)

export const queryRolePermission = (roleId, appId, params) => getAction(serviceApi.auth + `/permissions/roles/${roleId}/apps/${appId}`, params)

export const queryRoleLeafPermissionOfApp = (roleId, appId, params) => getAction(serviceApi.auth + `/permissions/left-app/roles/${roleId}/apps/${appId}`, params)

export const queryRoleLeafPermission = (roleId, appId, params) => getAction(serviceApi.auth + `/permissions/left/roles/${roleId}/apps/${appId}`, params)

// 保存授权分配信息
export const saveTenantRoleAppPermission = (params) => postAction(serviceApi.auth + '/permissions/tenant-role-permission', params);
